<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Member extends Admin_Controller
{
    function __construct ()
    {
        parent::__construct();
        $this->load->model('setting_m');
        $this->load->library('pagination');
    }

    public function index($page=1)
    {
    	$order = $this->input->get('order')?$this->input->get('order'):'desc';
    	//分页
        $limit = 15;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['total_rows'] = $this->user_m->count_user_search(1);
        $config['base_url'] = site_url('manage/member/index');
        $config['per_page'] = $limit;
        $config['num_links'] = 1;
        $start = ($page-1)*$limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['users'] = $this->user_m->get_user_group_rule_list_search_ord($start, $limit, $order,1);
        $this->load->view('member/index', $data);

    }
    public function add(){
        $usergroup = $this->authGroup_m->get_all_groups(1,1);
        $data['usergroup'] = $usergroup;
        $this->load->view('member/form', $data);
    }
    public function edit($uid)
    {
        if ($uid) {
            $member = $this->user_m->get_user_by_user_id_group($uid);
        } else {
            show_message('参数错误',site_url(X_URL.'member/index'),1);
        }
        $usergroup = $this->authGroup_m->get_all_groups(1,1);
        $data['usergroup'] = $usergroup;
        $data['member'] = $member;
//        pp($data);exit();
        $this->load->view('member/form', $data);
    }

    public function update()
    {
        $post = $this->input->post();
        $uid = isset($post['user_id']) ? intval($post['user_id']) : false;
        $username = isset($post['user']) ? htmlspecialchars($post['user'], ENT_QUOTES) : '';
        $group_id = isset($post['user_group']) ? intval($post['user_group']) : false;
        if (!$group_id) {
            show_message('请给用户选择用户组',site_url(X_URL.'member/add'),1);
        }
        $salt =get_salt();
        $getpassword= $this->input->post('user_password',true);
        if (!$uid) {
            $admin = array(
                'user_name' => $username,
                'user_password' =>  password_dohash($getpassword,$salt),
                'user_salt' => $salt,
                'user_credit' => $this->config->item('credit_start'),
                'user_regip' => get_onlineip(),
                'user_money'=> 0,
                'user_group'=> $group_id,
                'user_regtime' => time(),
                'user_logtime' => time(),
                'user_lock'=>0,
                'user_sex' =>$post['user_sex'] ?? '',
                'user_tel' =>$post['user_tel'] ?? '',
                'user_email' =>$post['user_email'] ?? '',
            );

            if(!$this->user_m->check_register_username($username)){
                $this->user_m->register($admin);
                $new_id = $this->db->insert_id();
            }
            $this->user_m->update_user($new_id,array('user_key'=>password_dohash($new_id,$salt)));
            $this->groupAcess_m->insert_user_access(['uid'=>$new_id,'group_id'=>$group_id]);
            show_message('新增用户成功',site_url(X_URL.'member/index'),1);
        } else {
            // 修改
            $admin = array(
                'user_group'=> $group_id,
                'user_sex' =>$post['user_sex'] ?? '',
                'user_tel' =>$post['user_tel'] ?? '',
                'user_email' =>$post['user_email'] ?? '',
            );
            if(!empty($getpassword)){
                $admin['user_salt'] = $salt;
                $admin['user_password'] = password_dohash($getpassword,$salt);
            }
            if(!empty($username)){
                $admin['user_name'] = $username;
            }
            $this->user_m->update_user($uid,$admin);
            $this->groupAcess_m->update_user_access($uid,['group_id'=>$group_id]);
            show_message('修改用户成功',site_url(X_URL.'member/index'),1);

        }
    }

    public function del()
    {
    	$user_id = $this->input->post('user_id');
    	if($user_id == 1){
    		echo json_encode(['status'=>2,"info"=>"默认管理员无法删除"]);exit();
    	}else{
    		if($this->user_m->delete_shop_users_by(array('user_id' => $user_id))){
    			echo json_encode(['status'=>1,"info"=>"删除成功"]);exit();
    		}else{
    			echo json_encode(['status'=>2,"info"=>"删除失败"]);exit();
    		}    		
    	}
    	
    }

}
